﻿using BaseUI;
using BLL;
using Model;
using System;

namespace Thksoft.Module.ParameterSettings
{
    public partial class AnnouncementEdit : BasePage
    {
        private SYS_ANNOUNCEMENTBLL amentBLL = new SYS_ANNOUNCEMENTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    BindData();
                }
            }
        }

        /// <summary>
        /// 修改绑定数据
        /// </summary>
        public void BindData()
        {
            string id = Request.QueryString["ID"].ToString();

            SYS_ANNOUNCEMENT model = amentBLL.SelectById(id);

            if (model != null)
            {
                FORM_COMPANY_ID.Value = model.N_COMPANYID;
                txtEndTime.Value = model.D_ENDTIME.ToString("yyyy-MM-dd");
                txtName.Text = model.C_NAME.ToString();
                txtContent.Value = model.C_CONTENT.ToString();
                FORM_REMARK.Value = model.C_REMARK.ToString();
                FORM_STATE.SelectedValue = model.N_STATE.ToString();
                FORM_TYPE.SelectedValue = model.N_TYPE.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 编辑条件

            if (FORM_COMPANY_ID.Value.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择公司");
                return;
            }
            if (FORM_TYPE.Text.Trim() == "")
            {
                MessageBox.Show(Page, "公告类型不能为空");
                return;
            }
            if (txtEndTime.Value.Trim() == "")
            {
                MessageBox.Show(Page, "到期时间不能为空");
                return;
            }
            if (FORM_STATE.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择公告状态");
                return;
            }
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show(Page, "公告标题不能为空");
                return;
            }
            if (txtContent.Value.Trim() == "")
            {
                MessageBox.Show(Page, "公告内容不能为空");
                return;
            }

            #endregion 编辑条件

            SYS_ANNOUNCEMENT model = new SYS_ANNOUNCEMENT();

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //修改
                string id = Request.QueryString["ID"].ToString();

                model = amentBLL.SelectById(id);
            }
            else
            {
                model.N_ID = 1;
            }

            model.C_CONTENT = txtContent.Value.ToString();
            model.C_NAME = txtName.Text.ToString();
            model.N_COMPANYID = Convert.ToDecimal(FORM_COMPANY_ID.Value);
            model.D_ENDTIME = DateTime.Parse(txtEndTime.Value).AddDays(1).AddSeconds(-1);
            model.N_STATE = decimal.Parse(FORM_STATE.SelectedValue);
            model.C_REMARK = FORM_REMARK.Value.ToString();
            model.N_TYPE = Convert.ToDecimal(FORM_TYPE.SelectedValue);
            model.D_STARTTIME = DateTime.Now;
            model.C_URL = "/Module/ParameterSettings/AnnouncementDetail.aspx?ID=" + model.N_ID;

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //修改
                int count = amentBLL.UPDATE(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.Show(Page, "修改成功！");
                }
                else
                {
                    MessageBox.Show(Page, "修改失败！");
                }
            }
            else
            {
                model.N_USERID = CurrentUserID;
                model.C_REMARK += DateTime.Now;
                //新增
                int count = amentBLL.INSERT(model, lognUser.USER_NAME);
                if (count > 0)
                {
                    MessageBox.Show(Page, "新增成功！");
                }
                else
                {
                    MessageBox.Show(Page, "新增失败！");
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
        }
    }
}